/**
 * Project Name:template-web
 * File Name   :UrlTag.java
 * Package Name:org.cn.template.component.web
 * Date:2015年9月27日下午5:02:10
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.web;

import java.io.IOException;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;

/**
 * ClassName:UrlTag <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015年9月27日 下午5:02:10 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see 	 
 */
public class UrlTag implements TemplateDirectiveModel
{

	/*
	 * 默认请求后台
	 */
	private int		type	= 0;
	private String	url		= "";
	public static String[]suffix = {".jsp"};
	
	
	@Override
	public void execute(Environment env, @SuppressWarnings("rawtypes") Map map, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException,
			IOException {
		Object obj_type = map.get("type");
		if (obj_type instanceof TemplateNumberModel) {
			type = ((TemplateNumberModel) obj_type).getAsNumber().intValue();
		}
		Object obj_url = map.get("url");
		if(obj_url!=null){
			if(obj_url instanceof TemplateScalarModel){
				url =	((TemplateScalarModel)obj_url).getAsString();
			}
		}
		env.getOut().write(url+suffix[type]);  
	}
	
	public static String getUrl(String url) {
		return url+suffix[0];
	}

}
